// 路由配置

import { createRouter, createWebHashHistory } from 'vue-router'

// 引入页面
import Layout from '@/pages/Layout/index.vue'
import Home from '@/pages/Home/Home.vue'
import { useStore } from 'vuex'
// 懒加载页面
const Announcement = () => import('@/pages/Announcement/Announcement.vue')
const AnnouncementDetails = () => import('@/pages/Announcement/AnnouncementDetails.vue')
const PlatformTrends = () => import('@/pages/PlatformTrends/PlatformTrends.vue')
const PlatformTrendsDetails = () => import('@/pages/PlatformTrends/PlatformTrendsDetails.vue')
const PoliciesRegulations = () => import('@/pages/PoliciesRegulations/PoliciesRegulations.vue')
const PoliciesRegulationsDetails = () => import('@/pages/PoliciesRegulations/PoliciesRegulationsDetails.vue')
const Complain = () => import('@/pages/Complain/Complain.vue')
const ComplainDetails = () => import('@/pages/Complain/ComplainDetails.vue')
const Mine = () => import('@/pages/Mine/Mine.vue')
const PersonalInfo = () => import('@/pages/Mine/PersonalInfo.vue')
const EnterpriseInfo = () => import('@/pages/Mine/EnterpriseInfo.vue')
const ParticipateBidding  = () => import('@/pages/Mine/ParticipateBidding.vue')
const MyBid = () => import('@/pages/Mine/MyBid.vue')
const DataSubmit = () => import('@/pages/Mine/DataSubmit.vue')

// 定义一个routers数组，在里面定义路由规则
const routes = [
    {
        path: '',
        component: Layout,
        redirect: '/home',
        children: [
            {
                path: 'home',
                name: 'Home',
                component: Home,
                meta: {
                    title: '首页', // 面包屑显示的页面名称
                    behavior: "reset", // 记录面包屑处理方式
                    level: 1,
                    tabIndex: 0
                }
            },
            {
                path: 'details',
                name: 'Details',
                component: AnnouncementDetails,
                meta: {
                    title: '信息公告详情页',
                    behavior: "push",
                    level: 2,
                    tabIndex: 0
                }
            },
            {
                path: 'announcement',
                name: 'Announcement',
                component: Announcement,
                meta: {
                    title: '信息公告',
                    showFather: false,
                    behavior: "reset",
                    level: 2,
                    tabIndex: 0
                }
            },
            {
                path: 'announcementDetails',
                name: 'AnnouncementDetails',
                component: AnnouncementDetails,
                meta: {
                    title: '信息公告详情页',
                    behavior: "push",
                    level: 3,
                    tabIndex: 0
                }
            },
            {
                path: 'platformTrends',
                name: 'PlatformTrends',
                component: PlatformTrends,
                meta: {
                    title: '平台动态',
                    behavior: "reset",
                    level: 2,
                    tabIndex: 1
                }
            },
            {
                path: 'platformTrendsDetails',
                name: 'PlatformTrendsDetails',
                component: PlatformTrendsDetails,
                meta: {
                    title: '平台动态详情',
                    behavior: "push",
                    level: 3,
                    tabIndex: 1
                }
            },
            {
                path: 'policiesRegulations',
                name: 'PoliciesRegulations',
                component: PoliciesRegulations,
                meta: {
                    title: '政策法规',
                    behavior: "reset",
                    level: 2,
                    tabIndex: 2
                }
            },
            {
                path: 'policiesRegulationsDetails',
                name: 'PoliciesRegulationsDetails',
                component: PoliciesRegulationsDetails,
                meta: {
                    title: '政策法规详情',
                    behavior: "push",
                    level: 3,
                    tabIndex: 2
                }
            },
            {
                path: 'complain',
                name: 'Complain',
                component: Complain,
                meta: {
                    title: '违规投诉',
                    behavior: "reset",
                    level: 2,
                    tabIndex: 3
                }
            },
            {
                path: 'complainDetails',
                name: 'ComplainDetails',
                component: ComplainDetails,
                meta: {
                    title: '违规投诉详情页',
                    behavior: "push",
                    level: 3,
                    tabIndex: 3
                }
            },
            {
                path: 'mine',
                name: 'Mine',
                component: Mine,
                redirect: 'mine/personalInfo',
                children: [
                    {
                        path: 'personalInfo',
                        name: 'PersonalInfo',
                        component: PersonalInfo,
                        meta: {
                            title: '个人中心',
                            behavior: "reset",
                            level: 2,
                            tabIndex: 4,
                            mineIndex: 0
                        }
                    },
                    {
                        path: 'enterpriseInfo',
                        name: 'EnterpriseInfo',
                        component: EnterpriseInfo,
                        meta: {
                            title: '企业信息',
                            behavior: "reset",
                            level: 3,
                            tabIndex: 4,
                            mineIndex: 1
                        }
                    },
                    {
                        path: 'participateBidding',
                        name: 'ParticipateBidding',
                        component: ParticipateBidding,
                        meta: {
                            title: '参与投标',
                            behavior: "reset",
                            level: 3,
                            tabIndex: 4,
                            mineIndex: 2
                        }
                    },
                    {
                        path: 'myBid',
                        name: 'MyBid',
                        component: MyBid,
                        meta: {
                            title: '我的投标',
                            behavior: "reset",
                            level: 3,
                            tabIndex: 4,
                            mineIndex: 3
                        }
                    },
                    {
                        path: 'dataSubmit',
                        name: 'DataSubmit',
                        component: DataSubmit,
                        meta: {
                            title: '投标资料提交',
                            behavior: "push",
                            level: 4,
                            tabIndex: 4,
                            mineIndex: 3
                        }
                    }
                ]
            }
        ]
    }
]

// 创建路由实例，并配置路由模式和上面定义的路由规则
const router = createRouter({
    history: createWebHashHistory(),
    routes
})

// 路由前置守卫
router.beforeEach((to, from) => {
    console.log('路由前置守卫', to, from);
    // 通过vuex管理面包屑路径
    const store = useStore()
    if (to.meta.behavior === 'push') {
        store.commit('pushPath', to)
    } else if (to.meta.behavior === 'reset') {
        store.commit('resetPath', to)
    }
    console.log('BCPath', store.state.bcPath)
    return true
})

// 导出路由实例
export default router